/**
 * 
 */
package com.jbat.gwt.tiledmapcreator.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.List;

/**
 * 
 */
public class GwtTestImageLocatorServiceAsync extends GWTTestCase {

  private ImageLocatorServiceAsync imageLocatorSvc;

  @Override
  public String getModuleName() {
    return "com.jbat.gwt.tiledmapcreator.TiledMapCreator";
  }

  public void testGetImageNames() {
    imageLocatorSvc = GWT.create(ImageLocatorService.class);
    String tileSet = "dungeon";

    // Set up the callback object.
    AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable caught) {
        fail("An unexpected exception was thrown.");
        finishTest();
      }

      @Override
      public void onSuccess(List<String> result) {
        assertTrue(!result.isEmpty());
        finishTest();
      }
    };

    // Make the call to the service.
    imageLocatorSvc.getImageNames(tileSet, GWT.getModuleName(), callback);
    delayTestFinish(1000);
  }

  public void testGetImageNamesNoSuchDirectory() {
    imageLocatorSvc = GWT.create(ImageLocatorService.class);
    final String tileSet = "NoneSuchTileSet";

    // Set up the callback object.
    AsyncCallback<List<String>> callback = new AsyncCallback<List<String>>() {
      @Override
      public void onFailure(Throwable caught) {
        assertEquals(true, caught instanceof NoSuchDirectoryException);
        assertEquals("No directory on server named " + tileSet, caught.getMessage());
        finishTest();
      }

      @Override
      public void onSuccess(List<String> result) {
        fail("Expected exception not thrown.");
        finishTest();
      }
    };

    // Make the call to the service.
    imageLocatorSvc.getImageNames(tileSet, GWT.getModuleName(), callback);
    delayTestFinish(1000);
  }

}
